package com.bwie.spu.service.impl;

import com.bwie.spu.domain.po.*;
import com.bwie.spu.domain.vo.DetailAttrVo;
import com.bwie.spu.domain.vo.ProductDetailVo;
import com.bwie.spu.mapper.ProductDetailMapper;
import com.bwie.spu.service.ProductDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ProductDetailServiceImpl implements ProductDetailService {
    /**
     * 注入
     */
    @Autowired
    private ProductDetailMapper mapper;
    @Autowired
    private ThreadPoolExecutor itemThreadPoolExcuter;

    /**
     * 根据skuId获得商品详情
     * @param skuId
     * @return
     */
    @Override
    public ProductDetailVo getProductDetail(Integer skuId) throws ExecutionException, InterruptedException {
        //创建返回值
        ProductDetailVo productDetailVo = new ProductDetailVo();

        //查询sku详情
        CompletableFuture<SkuPo> skuPoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuPo skuPo = mapper.getSkuById(skuId);
            //赋值
            productDetailVo.setSkuPo(skuPo);
            return skuPo;
        }, itemThreadPoolExcuter);

        //获得所有sku图片
        CompletableFuture<Void> skuImages = CompletableFuture.runAsync(() -> {
            List<PmsSkuImgPo> images = mapper.getPmsSkuImgPoBySkuId(skuId);
            //赋值
            productDetailVo.setPmsSkuImgPo(images);
        }, itemThreadPoolExcuter);

        //获得所有的商品属性信息
        CompletableFuture<Void> skuAttrs = CompletableFuture.runAsync(() -> {
            List<AttrPo> listAttrPos = mapper.getSkuSaleAttrs(skuId);
            //转换格式
            List<DetailAttrVo> listAttrPo = getSpuAttrVo(listAttrPos);
            //赋值
            productDetailVo.setAttrPos(listAttrPo);
        }, itemThreadPoolExcuter);

        //查询商品介绍商品介绍
        CompletableFuture<Void> skuTroduces = skuPoCompletableFuture.thenAcceptAsync((skuPo) -> {
            PmsSpuInfoDesc spuDecript = mapper.getSpuDecript(skuPo.getSpuId());
            //赋值
            productDetailVo.setDecript(spuDecript);
        }, itemThreadPoolExcuter);

        //获得所有的规格信息
        CompletableFuture<Void> skuTypes = skuPoCompletableFuture.thenAcceptAsync((skuPo) -> {
            List<GroupAttrPo> groupAttrPos = mapper.getGroupInfo(skuPo.getCatalogId());
            //赋值
            productDetailVo.setGroupAttrPos(groupAttrPos);
        }, itemThreadPoolExcuter);

        //堵塞
        CompletableFuture.allOf(
                skuPoCompletableFuture,
                skuImages,
                skuAttrs,
                skuTroduces,
                skuTypes
        ).get();




        //返回
        return productDetailVo;
    }












    //转换格式
    private List<DetailAttrVo> getSpuAttrVo(List<AttrPo> listAttrPos) {
        //创建返回值
        List<DetailAttrVo> list = new ArrayList<>();
        listAttrPos.forEach(attrPo -> {
            //创建AttrVo
            DetailAttrVo attrVo = new DetailAttrVo();
            //创建String集合
            List<String> strings = new ArrayList<>();
            attrVo.setAttrId(attrPo.getAttrId());
            attrVo.setAttrName(attrPo.getAttrName());
            //切割value
            String[] split = attrPo.getAttrValues().split(",");
            //循环
            for (int i = 0; i < split.length; i++) {
                strings.add(split[i]);
            }
            //赋值
            attrVo.setAttrValue(strings);
            //添加
            list.add(attrVo);
        });
        //返回
        return list;
    }
}
